/**
 * Created by netsnower on 15/3/8.
 */
//JS操作cookies方法!
    //写cookies
function setCookie(name,value)
{
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//读取cookies
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}
//删除cookies
function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

//获取地址栏hash
function GetHashString(name)
{
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.hash.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

//数据交互接口
function sendRequest(url,callback,postData) {
    //alert(postData);
    var req = createXMLHTTPObject();
    if (!req) return;
    var method = (postData) ? "POST" : "GET";
    req.open(method,url,true);
    req.setRequestHeader('User-Agent','XMLHTTP/1.0');
    if (postData)
        req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    req.onreadystatechange = function () {
        if (req.readyState != 4) return;
        if (req.status != 200 && req.status != 304) {
            //alert('HTTP error ' + req.status);
            return;
        }
        callback(req);
    }
    if (req.readyState == 4) return;
    req.send(postData);
}

var XMLHttpFactories = [
    function () {return new XMLHttpRequest()},
    function () {return new ActiveXObject("Msxml2.XMLHTTP")},
    function () {return new ActiveXObject("Msxml3.XMLHTTP")},
    function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
    var xmlhttp = false;
    for (var i=0;i<XMLHttpFactories.length;i++) {
        try {
            xmlhttp = XMLHttpFactories[i]();
        }
        catch (e) {
            continue;
        }
        break;
    }
    return xmlhttp;
}
//添加样式
function addClass(elm,newClass){
    var classes = elm.className.split(' ');
    var classIndex=hasClass(elm,newClass);
    if(classIndex==-1){
        classes.push(newClass);
    };
    elm.className = classes.join(' ');
}

function hasClass(elm,className){
    var classes = elm.className.split(' ');
    for(var a in classes){
        if(classes[a]==className){
            return a;
        };
    }
    return -1;
}
function removeClass(elm,className){
    var classes = elm.className.split(' ');
    var classIndex=hasClass(elm,className);
    if(classIndex!=-1){
        classes.splice(classIndex,1);
    };
    elm.className=classes.join(' ');
}
//获取dom
function getEl(id) {
    return document.getElementById(id);
}